package com.xjrsoft.module.customer.erp_model.unitConvertManage.service.impl;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xjrsoft.module.customer.erp_model.unitConvertManage.entity.ErpUnitconvert;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.xjrsoft.module.customer.erp_model.unitConvertManage.entity.ErpUnitconvertchild;
import com.xjrsoft.module.customer.erp_model.unitConvertManage.mapper.ErpUnitconvertchildMapper;
import com.xjrsoft.module.customer.erp_model.unitConvertManage.dto.ErpUnitconvertListDto;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.ConventPage;
import com.xjrsoft.common.page.PageOutput;
import java.util.List;
import com.xjrsoft.module.customer.erp_model.unitConvertManage.mapper.ErpUnitconvertMapper;
import com.xjrsoft.module.customer.erp_model.unitConvertManage.service.IErpUnitconvertService;
import com.xjrsoft.core.mp.base.BaseService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

/**
 *  服务实现类
 *
 * @author pp
 * @since 2021-10-13
 */
@Service
@AllArgsConstructor
public class ErpUnitconvertServiceImpl extends BaseService<ErpUnitconvertMapper, ErpUnitconvert> implements IErpUnitconvertService {

	private ErpUnitconvertchildMapper erpUnitconvertchildMapper;

	@Override
	public IPage<ErpUnitconvert> getPageList(ErpUnitconvertListDto pageListDto) {
		Wrapper<ErpUnitconvert> wrapper = Wrappers.<ErpUnitconvert>query().lambda();
				return this.page(ConventPage.getPage(pageListDto), wrapper);
	}

	@Override
	public String addErpUnitconvert(ErpUnitconvert erpUnitconvert, List<ErpUnitconvertchild> erpUnitconvertchildList) {
		String erpUnitconvertId = IdWorker.get32UUID();
		erpUnitconvert.setFId(erpUnitconvertId);
		erpUnitconvertchildList.forEach(erpUnitconvertchild -> erpUnitconvertchild.setFConvertid(erpUnitconvertId));
		this.saveBatch(erpUnitconvertchildList, ErpUnitconvertchild.class, ErpUnitconvertchildMapper.class);
		boolean isSuccess = this.save(erpUnitconvert);
		return isSuccess ? erpUnitconvertId : null;
	}

	@Override
	public boolean updateErpUnitconvert(String id, ErpUnitconvert erpUnitconvert, List<ErpUnitconvertchild> erpUnitconvertchildList) {
		erpUnitconvert.setFId(id);
		erpUnitconvertchildList.forEach(erpUnitconvertchild -> erpUnitconvertchild.setFConvertid(id));
		erpUnitconvertchildMapper.delete(Wrappers.<ErpUnitconvertchild>query().lambda().eq(ErpUnitconvertchild::getFConvertid, id));
		this.saveBatch(erpUnitconvertchildList, ErpUnitconvertchild.class, ErpUnitconvertchildMapper.class);
		return this.updateById(erpUnitconvert);
	}

	public List<ErpUnitconvertchild> getErpUnitconvertchildByParentId(String parentId){
		Wrapper wrapper = Wrappers.<ErpUnitconvertchild>query().lambda().eq(ErpUnitconvertchild::getFConvertid, parentId);
		return erpUnitconvertchildMapper.selectList(wrapper);
	}
}
